package com.zr.kufangkuwei.controller;

import com.zr.kufangkuwei.pojo.*;
import com.zr.kufangkuwei.service.KuFangService;
import com.zr.meiju.DepotareaEnum;
import com.zr.meiju.XiaLaVo;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 马冲  库房库位
 * Created by Administrator on 2019/7/16.
 */
@RestController
@RequestMapping("kufangkuwei/")
public class KuFangController {

    @Autowired
    private KuFangService kuFangService;

    /**
     * 库区明细列表（三表联查）
     * @param depotareaSelectVo
     * @return
     */
    @PostMapping("queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody DepotareaSelectVo depotareaSelectVo){
        return kuFangService.queryPage(depotareaSelectVo);
    }

    /**
     * 修改接口
     * @param depotpositionUpdate
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    public ResultVo update(@RequestBody @Valid DepotpositionUpdate depotpositionUpdate, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return kuFangService.update(depotpositionUpdate);
    }

    /**
     * 新增的接口
     * @param depotareaAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public ResultVo add(@RequestBody @Valid DepotareaAddVo depotareaAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return kuFangService.add(depotareaAddVo);
    }


    /**
     * 库房编码下拉框
     * @param id
     * @return
     */
    @RequestMapping("/queryDepot")
    public ResultVo<List<Depot>> queryDepot(@RequestParam("id")Integer id){
        return kuFangService.queryDepot(id);
    }

    /**
     * 库区编码下拉框
     * @param id
     * @return
     */
    @RequestMapping("/queryDepotarea")
    public ResultVo<List<Depotarea>> queryDepotarea(@RequestParam("id")Integer id){
        return kuFangService.queryDepotarea(id);
    }

    /**
     * 启用禁用的下拉框
     * @return
     */
    @RequestMapping("/nianxianDropdownBox")
    public ResultVo<List<XiaLaVo>> nianxianDropdownBox(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< DepotareaEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setValue(i);
            xiaLaVo.setLabel(DepotareaEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }


    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("InquiryIntroduction")
    public ResultVo importPlanClass(MultipartFile file)throws Exception{
        return kuFangService.importPlanClass(file);
    }
}
